package com.wdz.http.exceptionMonitor;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.hutool.log.level.Level;
import com.wdz.http.Response;
import com.wdz.http.exception.LoginException;
import com.wdz.http.exception.NoPermissionException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局异常监听类
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(Exception.class)
    public Response exception(Exception e){
        log.error("异常监听-错误信息："+e.getMessage());
        e.printStackTrace();
        log.error(e.getMessage(), Level.ERROR);
        return Response.error504(e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(LoginException.class)
    public Response loginException(Exception e){
        log.error("异常监听-错误信息："+e.getMessage());
        e.printStackTrace();
        log.error(e.getMessage(), Level.ERROR);
        return Response.userInvalid(e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(NoPermissionException.class)
    public Response NoPermissionException(Exception e){
        log.info("用户没有权限访问");
        return Response.noPermission(e.getMessage());
    }
}
